﻿using System;
using System.Web.Mvc;

namespace VictorBlaga.DocumentManagement.Helpers
{
    /// <summary>
    /// Containts method that help create a SelectList from vairous other collections.
    /// </summary>
    public static class SelectListHelper
    {
        /// <summary>
        /// Created this method because SelectList constructor no longer accepts
        /// a null collection as parameter.
        /// </summary>
        /// <param name="array">The datasource array for the select list</param>
        /// <param name="valueField">Name of value field property.</param>
        /// <param name="displayField">Name of display filed property.</param>
        /// <returns></returns>
        public static SelectList CreateSelectList(Array array, string valueField, string displayField)
        {
            //The SelectList constructor does not accept a null collection anymore as of(ASP.NET MVC RC2)
            //Bug maybe?
            if (array == null)
                return new SelectList(new object[0]);
            return new SelectList(array, valueField, displayField);
        }
    }
}
